package com.zut.subject.application.controller;


import com.google.common.base.Preconditions;
import com.zut.subject.application.convert.convertSubjectInfo;
import com.zut.subject.application.dto.SubjectInfoDto;
import com.zut.subject.common.entity.PageResult;
import com.zut.subject.common.entity.Result;
import com.zut.subject.domain.entity.SubjectInfoBo;
import com.zut.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDto subjectInfoDto){

        Result result =new Result();

        if (log.isInfoEnabled()){
            log.info("传入数据为：{}",subjectInfoDto);
        }
        try {
            //传入参数校验
            Preconditions.checkNotNull(subjectInfoDto.getSubjectDifficult(),"您传入的难度为空");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDto.getSubjectName()),"您传入的题目名字为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectType(),"您传入的题目类型为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectScore(),"您传入的题目分数为空");

            //转化为bo类型
            SubjectInfoBo subjectInfoBo = convertSubjectInfo.INSTANCE.toSubjectInfoBo(subjectInfoDto);
            if (log.isInfoEnabled()){
                log.info("向domain传入数据为：{}",subjectInfoBo);
            }
            //向domain传参数
            subjectInfoDomainService.add(subjectInfoBo);
            return result.success(true);
        } catch (Exception exception) {
            //如果前端传入参数为空，抛出异常提示
            log.error(exception.getMessage());
            return result.fail(false);
        }
    }

    /**
     * 查询题目列表
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDto>> getSubjectPage(@RequestBody SubjectInfoDto subjectInfoDto){

        Result result =new Result();
        PageResult<SubjectInfoDto> pageResult=new PageResult<SubjectInfoDto>();


        if (log.isInfoEnabled()){
            log.info("传入数据为：{}",subjectInfoDto);
        }
        try {
            //参数校验
            Preconditions.checkNotNull(subjectInfoDto.getSubjectDifficult(),"您传入的难度为空");
            Preconditions.checkNotNull(subjectInfoDto.getLabelId(),"您传入的标签id为空");
            Preconditions.checkNotNull(subjectInfoDto.getCategoryId(),"您传入的题目分类id为空");
            Preconditions.checkNotNull(subjectInfoDto.getPageNo(),"您传入的页码为空");
            Preconditions.checkNotNull(subjectInfoDto.getPageSize(),"您传入的页面大小为空");

            SubjectInfoBo subjectInfoBo = convertSubjectInfo.INSTANCE.toSubjectInfoBo(subjectInfoDto);
            if (log.isInfoEnabled()){
                log.info("向domain传入数据为：{}",subjectInfoBo);
            }
            //传入的bo数据吐出查询出来的boList列表
            List<SubjectInfoBo> subjectInfoBoList=subjectInfoDomainService.getSubjectPage(subjectInfoBo);
            if (log.isInfoEnabled()){
                log.info("subjectInfoBoList:{}",subjectInfoBoList.get(0).getPageNo());
            }
            if (log.isInfoEnabled()){
                log.info("subjectInfoBoList:{}",subjectInfoBoList);
            }
            if (log.isInfoEnabled()){
                log.info("subjectInfoBoList:{}",subjectInfoBoList.get(0).getPageSize());
            }
            //转化为dtoList
            List<SubjectInfoDto> subjectInfoDtoList = convertSubjectInfo.INSTANCE.toListSubjectInfoDto(subjectInfoBoList);
            //运用封装的ResultPage，将里面必须的参数修改传出去
            pageResult.setPageNo(subjectInfoDto.getPageNo());
            if (log.isInfoEnabled()){
                log.info("pageNo:{}",subjectInfoDto.getPageNo());
            }
            pageResult.setPageSize(subjectInfoDto.getPageSize());
            pageResult.setRecord(subjectInfoDtoList);

            //查询成功返回封装好的结果
            return result.success(pageResult);
        } catch (Exception exception) {
            log.error(exception.getMessage());
            return result.fail(false);
        }
    }

    /**
     * 查询题目详情
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/subject/querySubjectInfo")
    public Result<Boolean> querySubjectInfo(@RequestBody SubjectInfoDto subjectInfoDto){

        Result result =new Result();

        if (log.isInfoEnabled()){
            log.info("传入数据为：{}",subjectInfoDto);
        }
        try {
            //传入参数校验
            Preconditions.checkNotNull(subjectInfoDto.getId(),"您传入的题目id为空");
            //转化为bo类型
            SubjectInfoBo subjectInfoBo = convertSubjectInfo.INSTANCE.toSubjectInfoBo(subjectInfoDto);
            if (log.isInfoEnabled()){
                log.info("向domain传入数据为：{}",subjectInfoBo);
            }
            //向domain传参数
            SubjectInfoBo Bo =subjectInfoDomainService.querySubjectInfo(subjectInfoBo);
            SubjectInfoDto Dto = convertSubjectInfo.INSTANCE.toSubjectInfoDto(Bo);

            return result.success(Dto);
        } catch (Exception exception) {
            //如果前端传入参数为空，抛出异常提示
            log.error(exception.getMessage());
            return result.fail(false);
        }
    }

}
